home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Sound / Playing & Recording / Macintosh Tracker / Trecker Server Folder / mac_event.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-22  |  9.3 KB  |  337 lines  |  [TEXT/KAHL]

  1. /* mac_event.c */
  2.  
  3. #include "mac_event.h"
  4. #include <Sound.h>
  5.  
  6.  
  7. Boolean                                ReceivedOpenEventFlag = false;
  8. char                                    FakeKeyBuffer[MAXKEYS];
  9. int                                        KeyBufPtr = 0;
  10.  
  11. Boolean                                QuitPending = false;
  12.  
  13. /* parameters controlling the synthesis, with handy default values. */
  14. short                                    AntiAliasing = true;
  15. short                                    StereoOn = true;
  16. unsigned short                SamplingRate = 22254;
  17. short                                    NumRepeats = 1;
  18. short                                    Speed = 50;
  19. short                                    StereoMix = 0;
  20. short                                    Loudness = 64;
  21.  
  22. int                                        RecalibratePlayer = false; /* set when settings change */
  23.  
  24.  
  25. FSSpec                                GlobalFileSpec;
  26.  
  27. ProcessSerialNumber        WhoLaunchedUs;
  28.  
  29.  
  30. /* function to check to see that all required parameters have been gotten */
  31. OSErr        MyGotRequiredParams(AppleEvent* theAppleEvent)
  32.     {
  33.         DescType        ReturnedType;
  34.         Size                ActualSize;
  35.         OSErr                Error;
  36.  
  37.         Error = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
  38.             &ReturnedType,NULL,0,&ActualSize);
  39.         if (Error == errAEDescNotFound)
  40.             {
  41.                 return noErr;  /* we got all the params, since no more were found */
  42.             }
  43.          else
  44.             {
  45.                 if (Error == noErr)
  46.                     {
  47.                         BUG("\pDidn't get all required parameters from apple event");
  48.                         return errAEEventNotHandled;  /* missed some, so it failed */
  49.                     }
  50.                  else
  51.                     {
  52.                         BUG("\pAEGetAttributePtr failed in MyGotRequiredParams");
  53.                         return Error;  /* AEGetAttributePtr failed, so we return why */
  54.                     }
  55.             }
  56.     }
  57.  
  58.  
  59. pascal OSErr MyHandleODoc(AppleEvent* theAppleEvent, AppleEvent* reply, long handlerRefcon)
  60.     {
  61.         OSErr                Error;
  62.         FSSpec            MyFSS;  /* place to put file info */
  63.         long                Index,ItemsInList;
  64.         AEDescList    DocList;
  65.         Size                ActualSize;
  66.         AEKeyword        Keywd;
  67.         DescType        ReturnedType;
  68.  
  69.         /* get the direct parameter--a descriptor list--and put it into DocList */
  70.         Error = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &DocList);
  71.         if (Error != noErr)
  72.             {
  73.                 BUG("\pCouldn't get file list");
  74.             }
  75.         if (Error != noErr) return Error;
  76.  
  77.         /* now we read all the additional parameters that MIGHT be present */
  78.         /* determining how to do the synthesis.  They won't be present if the special */
  79.         /* interface program was not used to launch us.  In that case the defaults */
  80.         /* will take over. */
  81.  
  82.         Error = AEGetParamPtr(theAppleEvent,keyAntiAliasing,typeShortInteger,
  83.             &ReturnedType,(void*)&AntiAliasing,sizeof(short),&ActualSize);
  84.         if (Error != noErr)
  85.             {
  86.                 BUG("\pBad keyAntiAliasing"); /* for debugging--don't worry about it */
  87.             }
  88.  
  89.         Error = AEGetParamPtr(theAppleEvent,keyStereoOn,typeShortInteger,
  90.             &ReturnedType,(void*)&StereoOn,sizeof(short),&ActualSize);
  91.         if (Error != noErr)
  92.             {
  93.                 BUG("\pBad keyStereoOn");
  94.             }
  95.  
  96.         Error = AEGetParamPtr(theAppleEvent,keySamplingRate,typeShortInteger,
  97.             &ReturnedType,(void*)&SamplingRate,sizeof(short),&ActualSize);
  98.         if (Error != noErr)
  99.             {
  100.                 BUG("\pBad keySamplingRate");
  101.             }
  102.  
  103.         Error = AEGetParamPtr(theAppleEvent,keyNumRepeats,typeShortInteger,
  104.             &ReturnedType,(void*)&NumRepeats,sizeof(short),&ActualSize);
  105.         if (Error != noErr)
  106.             {
  107.                 BUG("\pBad keyNumRepeats");
  108.             }
  109.  
  110.         Error = AEGetParamPtr(theAppleEvent,keySpeed,typeShortInteger,
  111.             &ReturnedType,(void*)&Speed,sizeof(short),&ActualSize);
  112.         if (Error != noErr)
  113.             {
  114.                 BUG("\pBad keySpeed");
  115.             }
  116.  
  117.         Error = AEGetParamPtr(theAppleEvent,keyStereoMix,typeShortInteger,
  118.             &ReturnedType,(void*)&StereoMix,sizeof(short),&ActualSize);
  119.         if (Error != noErr)
  120.             {
  121.                 BUG("\pBad keyStereoMix");
  122.             }
  123.  
  124.         Error = AEGetParamPtr(theAppleEvent,keyLoudness,typeShortInteger,
  125.             &ReturnedType,(void*)&Loudness,sizeof(short),&ActualSize);
  126.         if (Error != noErr)
  127.             {
  128.                 BUG("\pBad keyLoudness");
  129.             }
  130.  
  131.         Error = AEGetParamPtr(theAppleEvent,keySenderPSN,typeProcessSerialNumber,
  132.             &ReturnedType,(void*)&WhoLaunchedUs,sizeof(ProcessSerialNumber),&ActualSize);
  133.         if (Error != noErr)
  134.             {
  135.                 BUG("\pBad keySenderPSN");
  136.             }
  137.  
  138.         /* check for missing required parameters */
  139.         Error = MyGotRequiredParams(theAppleEvent);
  140.         if (Error != noErr)
  141.             {
  142.                 BUG("\Not all required params handled");
  143.             }
  144.         if (Error != noErr) return Error;
  145.         /* count the number of descriptor records in the list */
  146.         Error = AECountItems(&DocList,&ItemsInList);
  147.         /* now get each descriptor record from the list, coerce the returned data */
  148.         /* to an FSSpec record, and open the associated file */
  149.         for (Index=1; Index <= ItemsInList; Index += 1)
  150.             {
  151.                 Error = AEGetNthPtr(&DocList,Index,typeFSS,&Keywd,&ReturnedType,
  152.                     (void*)&MyFSS,sizeof(FSSpec),&ActualSize);
  153.                 if (Error == noErr)
  154.                     {
  155.                         GlobalFileSpec = MyFSS;
  156.                     }
  157.                  else
  158.                     {
  159.                         BUG("\pCouldn't get FSSpec out of type list in MyHandleODoc");
  160.                     }
  161.             }
  162.         Error = AEDisposeDesc(&DocList);
  163.  
  164.         ReceivedOpenEventFlag = true;
  165.  
  166.         return Error;
  167.     }
  168.  
  169.  
  170. /* this receives a simulated keypress from the other system.  The idea */
  171. /* was that we could simulate the user pressing '>', '<', and others by */
  172. /* sending apple events.  Unfortunately, since things are precomputed as far */
  173. /* ahead as we have memory for, the commands don't show up for many seconds. */
  174. /* If I ever get around to making this interrupt driven, well, the mechanism */
  175. /* is here ready and waiting. */
  176. pascal OSErr MyHandleKey(AppleEvent* theAppleEvent, AppleEvent* reply, long handlerRefcon)
  177.     {
  178.         OSErr                Error;
  179.         short                KeyPressed = 0; /* default key does nothing */
  180.         long                ActualSize;
  181.         DescType        ReturnedType;
  182.  
  183.         Error = AEGetParamPtr(theAppleEvent,keyKeyPressCharacter,typeShortInteger,
  184.             &ReturnedType,(void*)&KeyPressed,sizeof(short),&ActualSize);
  185.  
  186.         Error = AEGetParamPtr(theAppleEvent,keyAntiAliasing,typeShortInteger,
  187.             &ReturnedType,(void*)&AntiAliasing,sizeof(short),&ActualSize);
  188.         if (Error == noErr)
  189.             {
  190.                 RecalibratePlayer = true;
  191.             }
  192.  
  193.         Error = AEGetParamPtr(theAppleEvent,keyStereoOn,typeShortInteger,
  194.             &ReturnedType,(void*)&StereoOn,sizeof(short),&ActualSize);
  195.         if (Error != noErr)
  196.             {
  197.                 RecalibratePlayer = true;
  198.             }
  199.  
  200.         Error = AEGetParamPtr(theAppleEvent,keySamplingRate,typeShortInteger,
  201.             &ReturnedType,(void*)&SamplingRate,sizeof(short),&ActualSize);
  202.         if (Error != noErr)
  203.             {
  204.                 RecalibratePlayer = true;
  205.             }
  206.  
  207.         Error = AEGetParamPtr(theAppleEvent,keyNumRepeats,typeShortInteger,
  208.             &ReturnedType,(void*)&NumRepeats,sizeof(short),&ActualSize);
  209.         if (Error != noErr)
  210.             {
  211.                 RecalibratePlayer = true;
  212.             }
  213.  
  214.         Error = AEGetParamPtr(theAppleEvent,keySpeed,typeShortInteger,
  215.             &ReturnedType,(void*)&Speed,sizeof(short),&ActualSize);
  216.         if (Error != noErr)
  217.             {
  218.                 RecalibratePlayer = true;
  219.             }
  220.  
  221.         Error = AEGetParamPtr(theAppleEvent,keyStereoMix,typeShortInteger,
  222.             &ReturnedType,(void*)&StereoMix,sizeof(short),&ActualSize);
  223.         if (Error != noErr)
  224.             {
  225.                 RecalibratePlayer = true;
  226.             }
  227.  
  228.         Error = AEGetParamPtr(theAppleEvent,keyLoudness,typeShortInteger,
  229.             &ReturnedType,(void*)&Loudness,sizeof(short),&ActualSize);
  230.         if (Error != noErr)
  231.             {
  232.                 RecalibratePlayer = true;
  233.             }
  234.  
  235.         Error = noErr;
  236.  
  237.         if (Error == noErr)
  238.             {
  239.                 Error = MyGotRequiredParams(theAppleEvent);
  240.                 if (Error != noErr)
  241.                     {
  242.                         return Error;
  243.                     }
  244.                 if ((KeyBufPtr < MAXKEYS - 1) && (KeyPressed != 0))
  245.                     {
  246.                         FakeKeyBuffer[KeyBufPtr] = KeyPressed;
  247.                         KeyBufPtr += 1;
  248.                     }
  249.                 return noErr;
  250.             }
  251.          else
  252.             {
  253.                 return Error;
  254.             }
  255.     }
  256.  
  257.  
  258. /* forced quit event on system shutdown or user stoppage */
  259. pascal OSErr MyHandleQuit(AppleEvent* theAppleEvent, AppleEvent* reply, long handlerRefcon)
  260.     {
  261.         OSErr            Error;
  262.  
  263.         /* check for missing required parameters */
  264.         Error = MyGotRequiredParams(theAppleEvent);
  265.         if (Error != noErr) return Error;
  266.         QuitPending = true;
  267.         discard_buffer();
  268.         return noErr;
  269.     }
  270.  
  271.  
  272. /* this is a simple event loop.  Not having an interface, we don't pay any */
  273. /* attention to anything but apple events.  This also gives time to other */
  274. /* applications.  One improvement: adding checks for keyboard events might */
  275. /* be handy when the program is run without the interface.  They could just */
  276. /* be queued like they are done in MyHandleKey. */
  277. void            WaitForEvent(long SleepTime)
  278.     {
  279.         EventRecord                MyEvent;
  280.         OSErr                            Error;
  281.  
  282.         WaitNextEvent(everyEvent,&MyEvent,SleepTime,NULL);
  283.         switch (MyEvent.what)
  284.             {
  285.                 case kHighLevelEvent:
  286.                     Error = AEProcessAppleEvent(&MyEvent);
  287.                     break;
  288.                 default:
  289.                     break;
  290.             }
  291.     }
  292.  
  293.  
  294. /* whip off a message to the interface application explaining why the */
  295. /* song sounds like crap! */
  296. void                FatalError(short ErrorID)
  297.     {
  298.         short                            Error;
  299.         AppleEvent                Event;
  300.         AEAddressDesc            AddressDescriptor;
  301.         AppleEvent                Reply;
  302.  
  303.         Error = AECreateDesc(typeProcessSerialNumber,(void*)&WhoLaunchedUs,
  304.             sizeof(ProcessSerialNumber),&AddressDescriptor);
  305.         Error = AECreateAppleEvent(ControlEventClass,ErrorEvent,&AddressDescriptor,
  306.             kAutoGenerateReturnID,kAnyTransactionID,&Event);
  307.         Error = AEPutParamPtr(&Event,keyErrorIDNum,typeShortInteger,
  308.             (void*)&ErrorID,sizeof(short));
  309.         Error = AESend(&Event,&Reply,kAENoReply,kAENormalPriority,kNoTimeOut,NULL,NULL);
  310.     }
  311.  
  312.  
  313. int                    RegisterEventHandlers(void)
  314.     {
  315.         OSErr                Error;
  316.  
  317.         /* installing open document handler */
  318.         Error = AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,&MyHandleODoc,0,false);
  319.         if (Error != noErr)
  320.             {
  321.                 return false;
  322.             }
  323.  
  324.         Error = AEInstallEventHandler(ControlEventClass,ControlEvent,&MyHandleKey,0,false);
  325.         if (Error != noErr)
  326.             {
  327.                 return false;
  328.             }
  329.  
  330.         Error = AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,&MyHandleQuit,0,false);
  331.         if (Error != noErr)
  332.             {
  333.                 return false;
  334.             }
  335.         return true;
  336.     }
  337.